const Bpmn = require('bpmn-engine');
const EventEmitter = require('events').EventEmitter;
const fs = require('fs');

const engine = new Bpmn.Engine({
    source: fs.readFileSync('./ass/1003.bpmn'),
    moddleOptions: {
        camunda: require('camunda-bpmn-moddle/resources/camunda')
    }
});

let state;

// 开始执行
function centerExe(variables, callback) {
    const listener = new EventEmitter();
    // 流程执行的线
    listener.on('taken', (flow) => {
        if (callback instanceof Function) {
            callback(flow.id);
        }
    });
    // 基本执行
    engine.execute({
        variables: variables,
        listener: listener
    },(err, definition) => {
        if (err) {
            callback('error');
        };
        console.log('Bpmn definition definition started with id', definition.getProcesses()[0].context.variables.sum);
    });
}
// 中间节点执行
function centerExe(state, variables, callback) {
    const listener = new EventEmitter();
    listener.once('wait-UserTask_1fis4zu', (child, instance) => {
        console.log(child.activity.id);
        instance.signal(child.activity.id, {
            sirname: 'von Rosen'
        });
    });
    // 流程执行的线
    listener.on('taken', (flow) => {
        if (callback instanceof Function) {
            callback(flow.id);
        }
        console.log(flow.id);
    });
    const engine = Bpmn.Engine.resume(state, {
        listener: listener
    });
}
// 停止
function exe() {
    const listener = new EventEmitter();
    listener.once('wait-UserTask_1fis4zu', (child, instance) => {
       // engine.stop();
      //  state = engine.getState();
        console.log(`task <${child.activity.id}> was taken`);
        instance.signal(child.activity.id, {
            sirname: 'ddd'
        });
    });

    listener.once('wait-UserTask_09l4c4c', (child, instance) => {
       // state = engine.getState();
        console.log(`task <${child.activity.id}> was taken`);
        instance.signal(child.activity.id, {
            sirname: '333'
        });
    });
    listener.once('wait-UserTask_0ytm91g', (child, instance) => {
        // state = engine.getState();
        console.log(`task <${child.activity.id}> was taken`);
        instance.signal(child.activity.id, {
            sirname: '333'
        });
    });

    // 流程执行的线
    listener.on('taken', (flow) => {
        console.log(`flow <${flow.id}> was taken`);
    });

    engine.once('end', (definition) => {
        console.log(`User sirname is`);
    });

    const variables = {
        manager: 90,
        sum: 100
    };

    // 基本执行
    engine.execute({
        variables: variables,
        listener: listener
    },(err, definition) => {
        if (err) throw err;
        console.log('Bpmn definition definition started with id');
    });


    setTimeout(() => {
      //  const pending = engine.getPendingActivities();
      //  console.log(JSON.stringify(pending));
        // const task = pending.definitions[0].children.find(c => c.type === 'bpmn:UserTask');
        // engine.signal(task.id);
    }, 30);
}
exe();
setTimeout(() => {
  //  centerExe(state);
}, 100);